﻿using _百度数据表格.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace _百度数据表格
{
    /// <summary>
    /// PieDataCoursePassRate 的摘要说明
    /// </summary>
    public class PieDataCoursePassRateHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            Result res = new Result();

            using (var db = new BaseContext())
            {

                string[] CourseList = (db.StuFraction.Select(o => new
                {
                    o.CourseName
                }).Distinct()).ToArray().Select(o => o.CourseName).ToArray();

                string[] ExamDateList = db.StuFraction.Select(o => new
                {
                    o.ExamTime
                }).Distinct().ToList().Select(o => o.ExamTime.ToString("yyyy-MM")).ToArray();




                res.Course = CourseList;
                res.ExamDate = ExamDateList;
                res.CoursePassRate = new List<CoursePassRate>();

                for (int i = 0; i < CourseList.Count(); i++)
                {
                    CoursePassRate cpr = new CoursePassRate();
                    cpr.name = CourseList[i];
                    cpr.stack = "及格人数";
                    cpr.type = "line";
                    cpr.data = db.Student
                    .Join(
                       db.StuFraction,
                       a =>
                          new
                          {
                              Id = a.Id
                          },
                       b =>
                          new
                          {
                              Id = b.StuId
                          },
                       (a, b) =>
                          new
                          {
                              a = a,
                              b = b
                          }
                    )
                    .Where(
                       temp0 =>
                          (((temp0.a.Class == "高一") && (temp0.b.Score >= 60)) && (temp0.b.CourseName == cpr.name))
                    )
                    .GroupBy(
                       temp0 =>
                          new
                          {
                              CourseName = temp0.b.CourseName,
                              ExamTime = temp0.b.ExamTime
                          },
                       temp0 => temp0.b
                    )
                    .Select(
                       g =>
                          new
                          {
                              CourseName = g.Key.CourseName,
                              ExamTime = g.Key.ExamTime,
                              PassCount = g.Count()
                          }
                    ).Select(o => o.PassCount).ToArray();
                    res.CoursePassRate.Add(cpr);
                }

                string josn = JsonConvert.SerializeObject(res);

                context.Response.Clear();
                context.Response.ContentEncoding = System.Text.Encoding.UTF8;
                context.Response.Write(josn);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }

    public class Result
    {
        public string[] Course { get; set; }

        public string[] ExamDate { get; set; }

        public List<CoursePassRate> CoursePassRate { get; set; }
    }

    public class CoursePassRate
    {
        public string name { get; set; }
        public string type { get; set; }
        public string stack { get; set; }
        public int[] data { get; set; }
    }
}